home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / source / Vector3.cpp < prev    next >
C/C++ Source or Header  |  2005-11-18  |  3KB  |  173 lines

  1.  
  2. #include "Vector3.h"
  3.  
  4. namespace peon
  5. {
  6.     Vector3::Vector3( float x_, float y_, float z_ )
  7.     {
  8.         x = x_;
  9.         y = y_;
  10.         z = z_;
  11.     }
  12.  
  13.     Vector3::~Vector3()
  14.     {
  15.     }
  16.  
  17.     void Vector3::set( float x_, float y_, float z_ )
  18.     {
  19.         x = x_;
  20.         y = y_;
  21.         z = z_;
  22.     }
  23.  
  24.     float Vector3::length( void )
  25.     {
  26.         return( (float)sqrt( x * x + y * y + z * z ) );
  27.     }
  28.  
  29.     void Vector3::normalize( void )
  30.     {
  31.         float fLength = length();
  32.  
  33.         x = x / fLength;
  34.         y = y / fLength;
  35.         z = z / fLength;
  36.     }
  37.  
  38.     // Static utility methods...
  39.  
  40.     static float distance( const Vector3 &v1,  const Vector3 &v2  )
  41.     {
  42.         float dx = v1.x - v2.x;
  43.         float dy = v1.y - v2.y;
  44.         float dz = v1.z - v2.z;
  45.  
  46.         return (float)sqrt( dx * dx + dy * dy + dz * dz );
  47.     }
  48.  
  49.     static float dotProduct( const Vector3 &v1,  const Vector3 &v2 )
  50.     {
  51.         return( v1.x * v2.x + v1.y * v2.y + v1.z * v2.z  );
  52.     }
  53.  
  54.     static Vector3 crossProduct( const Vector3 &v1,  const Vector3 &v2 )
  55.     {
  56.         Vector3 vCrossProduct;
  57.  
  58.         vCrossProduct.x = v1.y * v2.z - v1.z * v2.y;
  59.         vCrossProduct.y = v1.z * v2.x - v1.x * v2.z;
  60.         vCrossProduct.z = v1.x * v2.y - v1.y * v2.x;
  61.  
  62.         return vCrossProduct;
  63.     }
  64.  
  65.     // Operators...
  66.  
  67.     Vector3 Vector3::operator + ( const Vector3 &other )
  68.     {
  69.         Vector3 vResult(0.0f, 0.0f, 0.0f);
  70.  
  71.         vResult.x = x + other.x;
  72.         vResult.y = y + other.y;
  73.         vResult.z = z + other.z;
  74.  
  75.         return vResult;
  76.     }
  77.  
  78.     Vector3 Vector3::operator + ( void ) const
  79.     {
  80.         return *this;
  81.     }
  82.  
  83.     Vector3 Vector3::operator - ( const Vector3 &other )
  84.     {
  85.         Vector3 vResult(0.0f, 0.0f, 0.0f);
  86.  
  87.         vResult.x = x - other.x;
  88.         vResult.y = y - other.y;
  89.         vResult.z = z - other.z;
  90.  
  91.         return vResult;
  92.     }
  93.  
  94.     Vector3 Vector3::operator - ( void ) const
  95.     {
  96.         Vector3 vResult(-x, -y, -z);
  97.  
  98.         return vResult;
  99.     }
  100.  
  101.     Vector3 Vector3::operator * ( const Vector3 &other )
  102.     {
  103.         Vector3 vResult(0.0f, 0.0f, 0.0f);
  104.  
  105.         vResult.x = x * other.x;
  106.         vResult.y = y * other.y;
  107.         vResult.z = z * other.z;
  108.  
  109.         return vResult;
  110.     }
  111.  
  112.     Vector3 Vector3::operator * ( const float scalar )
  113.     {
  114.         Vector3 vResult(0.0f, 0.0f, 0.0f);
  115.  
  116.         vResult.x = x * scalar;
  117.         vResult.y = y * scalar;
  118.         vResult.z = z * scalar;
  119.  
  120.         return vResult;
  121.     }
  122.  
  123.     Vector3 operator * ( const float scalar, const Vector3 &other )
  124.     {
  125.         Vector3 vResult(0.0f, 0.0f, 0.0f);
  126.  
  127.         vResult.x = other.x * scalar;
  128.         vResult.y = other.y * scalar;
  129.         vResult.z = other.z * scalar;
  130.  
  131.         return vResult;
  132.     }
  133.  
  134.     Vector3 Vector3::operator / ( const Vector3 &other )
  135.     {
  136.         Vector3 vResult(0.0f, 0.0f, 0.0f);
  137.  
  138.         vResult.x = x / other.x;
  139.         vResult.y = y / other.y;
  140.         vResult.z = z / other.z;
  141.  
  142.         return vResult;
  143.     }
  144.  
  145.     Vector3& Vector3::operator = ( const Vector3 &other )
  146.     {
  147.         x = other.x;
  148.         y = other.y;
  149.         z = other.z;
  150.  
  151.         return *this;
  152.     }
  153.  
  154.     Vector3& Vector3::operator += ( const Vector3 &other )
  155.     {
  156.         x += other.x;
  157.         y += other.y;
  158.         z += other.z;
  159.  
  160.         return *this;
  161.     }
  162.  
  163.     Vector3& Vector3::operator -= ( const Vector3 &other )
  164.     {
  165.         x -= other.x;
  166.         y -= other.y;
  167.         z -= other.z;
  168.  
  169.         return *this;
  170.     }
  171. }
  172.  
  173.